package com.leo;

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @ClassName SnowAnimationDemo
 * @Description 带音乐的雪花效果
 * @Author wangss
 * @date 2020.11.05 20:03
 * @Version 1.0
 */
public class SnowAnimationDemo extends Application {

    private List<Transition> transitions = new ArrayList<>();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane root = new AnchorPane();
        root.setStyle("-fx-background-color: #ffffff");

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(1500);
        primaryStage.setHeight(900);
        primaryStage.setFullScreen(true);
        primaryStage.setFullScreenExitHint("");
        primaryStage.centerOnScreen();
        primaryStage.setTitle("雪花飘落动画效果");
        primaryStage.show();

        StackPane sp = new StackPane();
        root.getChildren().addAll(sp);

        Node flowerView = getFlowerView(40, (int) scene.getWidth(), (int) scene.getHeight(), 200);

        ImageView imageView = new ImageView("images/timg.jpg");
        sp.getChildren().addAll(imageView, flowerView);

        URL url = getClass().getClassLoader().getResource("images/1.mp3");
        Media media = new Media(url.toExternalForm());
        MediaPlayer mp = new MediaPlayer(media);

        KeyCombination key = new KeyCodeCombination(KeyCode.Y, KeyCombination.CONTROL_DOWN);
        scene.getAccelerators().put(key, () -> {
            transitions.forEach(transition -> transition.play());
            mp.play();
        });
    }

    public Node getFlowerView(int number, int width, int height, int zz) {

        List<ImageView> list = new ArrayList<>();

        Random random = new Random();

        int x = 0;
        int y = 0;
        int z = 0;

        for (int i = 0; i < number; i++) {
            ImageView imageView = new ImageView("images/snow.png");
            imageView.setPreserveRatio(true);
            list.add(imageView);

            if (random.nextBoolean()) {
                x = random.nextInt(width) + random.nextInt(300) + 300;
            } else {
                x = random.nextInt(width) - random.nextInt(300) - 300;
            }

            y = random.nextInt(50);
            z = random.nextInt(zz);

            imageView.setTranslateX(x);
            imageView.setTranslateY(y);
            imageView.setTranslateZ(z);

            imageView.setOpacity(0);
        }

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #FFB6C100");
        anchorPane.getChildren().addAll(list);
        SubScene subScene = new SubScene(anchorPane, width, height, true, SceneAntialiasing.BALANCED);
        PerspectiveCamera camera = new PerspectiveCamera();
        subScene.setCamera(camera);

        list.forEach(view -> {
            double time = random.nextDouble() * 5 + 5;
            TranslateTransition tt = new TranslateTransition(Duration.seconds(time));
            tt.setFromX(view.getTranslateX());
            tt.setFromY(view.getTranslateY());
            tt.setByX(400);
            tt.setByY(height);

            FadeTransition ft1 = new FadeTransition(Duration.seconds(time / 2));
            ft1.setFromValue(0);
            ft1.setToValue(1);

            FadeTransition ft2 = new FadeTransition(Duration.seconds(time / 2));
            ft2.setFromValue(1);
            ft2.setToValue(0);

            RotateTransition rt = new RotateTransition(Duration.seconds(time));
            rt.setFromAngle(0);
            rt.setToAngle(360);

            SequentialTransition st = new SequentialTransition();
            st.getChildren().addAll(ft1, ft2);

            ParallelTransition parallelTransition = new ParallelTransition();
            parallelTransition.setNode(view);
            parallelTransition.setCycleCount(-1);
            parallelTransition.getChildren().addAll(tt, st, rt);

            transitions.add(parallelTransition);
        });


        return anchorPane;
    }
}
